<?php

/**
 * 库存管理
 * @package  htdocs
 * @subpackage  quanliadmin
 * @create by allen.qiang
 */
 
quanli_require('module/base/goodsHandler.class.php');
quanli_require('module/base/wmsHandler.class.php');

class pa_goodsStore {

    public function execute() {

        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //库存管理列表
                    $this->goodsStoreList();
                break;
    
                case 2: //查看商品仓库列表
                    $this->goodsStoreInfo();
                    break;

            }

        }

    }
    
    /**
     * 商品分仓库列表
     *
     */
	private function goodsStoreInfo(){
		global $G_PGLOB;
		
	    if(!$_REQUEST['goodsID'] || !is_numeric($_REQUEST['goodsID'])){
            $G_PGLOB->smarty->assign('error_msg','参数错误');
            $G_PGLOB->smarty->display('comm/errorDisplay.tpl');
            die();
        }

        //实例化进销存管理类
        $wmsHandler = new wmsHandler( $G_PGLOB->DBCA );

        //传参数组
        $c = array(
            'goodsID' => $_REQUEST['goodsID'],  //商品ID
            'storeGoodsInfo' => array() ,       //记录商品库存信息
            'channelGoodsInfo' => array()       //记录商品渠道库存比例信息
        );

        //查询仓库库存信息
        $result = $G_PGLOB->DBCA->stmt_query('SELECT wg.*,ws.`storeName`,gs.`goodsName`,gs.`product_code`,gs.`spec` FROM wms_goodsstore  wg
											  INNER JOIN goods_sale_edit gs ON gs.`goodsID`=wg.`goodsID`
											  INNER JOIN `wms_store` ws ON wg.`storeID`=ws.`storeID` WHERE wg.goodsID =? ' , 'i' , $_REQUEST['goodsID']);

        $c['storeGoodsInfo'] = $result;

        //统计可售数量总库存
        $enableSale_count = $G_PGLOB->DBCA->stmt_query( 'SELECT SUM(enableSaleAmount) enableSaleAmount FROM wms_goodsstore WHERE goodsID=?' , 'i' , $_REQUEST['goodsID'] );

        //统计锁定数量总库存
        $lock_count = $G_PGLOB->DBCA->stmt_query( 'SELECT SUM(lockAmount) lockAmount FROM wms_goodsstore WHERE goodsID=?' , 'i' , $_REQUEST['goodsID'] );

        //统计坏货数量总库存
        $bad_count = $G_PGLOB->DBCA->stmt_query( 'SELECT SUM(badAmount) badAmount FROM wms_goodsstore WHERE goodsID=?' , 'i' , $_REQUEST['goodsID'] );

        //统计销售数量总库存
        $sale_count = $G_PGLOB->DBCA->stmt_query( 'SELECT SUM(saleAmount) saleAmount FROM wms_goodsstore WHERE goodsID=?' , 'i' , $_REQUEST['goodsID'] );

        //此商品的总库存数
        $storageTotal = 0;
        $storageTotal += $enableSale_count[0]->enableSaleAmount;
        $storageTotal += $lock_count[0]->lockAmount;
        $storageTotal += $bad_count[0]->badAmount;
        $storageTotal += $sale_count[0]->saleAmount;

        //查询所有渠道
        $search_all_channel = 'SELECT * FROM 3rd_channel';
        $channelInfo        = $G_PGLOB->DBCA->stmt_query( $search_all_channel );

        //循环查询该商品在各个渠道的库存信息
        if( $channelInfo ){

            foreach ( $channelInfo as $channel ){

                //查询销售渠道库存比例信息(全局)
                $arg_array = array(
                    'channelID' => $channel->channelID
                );
                $percent_info = $wmsHandler->searchGoodsStorePercent( $arg_array );

                //查询渠道明细信息
                $detail_channel = array(
                    'goodsID' => $_REQUEST['goodsID'],
                    'channelID' => $channel->channelID
                );
                $channel_info = $wmsHandler->searchGoodsStoreChannel( $detail_channel );

                //如果在渠道明细表中查询到数据，则已查询到的数据为准
                if( $channel_info ){

                    $c['channelGoodsInfo'][] = array(
                        'goodsID' => $channel_info[0]['goodsID'],
                        'channelID' => $channel_info[0]['channelID'],
                        'channelName' => $channel_info[0]['channelName'],
                        'enableSaleAmount' => $channel_info[0]['enableSaleAmount'],
                        'saleAmount' => $channel_info[0]['saleAmount']
                    );

                } else { //若没查到明细数据，则查询该渠道比例分配信息

                    if( $percent_info ){  //如果该渠道存在分配比例，则按照比例和该商品的总库存，计算出该渠道的库存

                        //计算渠道比例下总库存数
                        $total_storage = round( $storageTotal * $percent_info[0]['percent'] / 100 );

                        $c['channelGoodsInfo'][] = array(
                            'goodsID' => $_REQUEST['goodsID'],
                            'channelID' => $channel->channelID,
                            'channelName' => $channel->channelName,
                            'enableSaleAmount' => $total_storage,
                            'saleAmount' => 0
                        );

                    } else {

                        $c['channelGoodsInfo'][] = array(
                            'goodsID' => $_REQUEST['goodsID'],
                            'channelID' => $channel->channelID,
                            'channelName' => $channel->channelName,
                            'enableSaleAmount' =>0,
                            'saleAmount' => 0
                        );

                    }

                }

            }

        }

        $G_PGLOB->smarty->assign('info' , $c);
        $G_PGLOB->smarty->display('wms/goodsStore.tpl');
	}
	
	/**
	 * 商品库存列表 
	 *
	 */
    private function goodsStoreList() {

        global $G_PGLOB;

        $G_PGLOB->smarty->assign( 'EDIT_PERMISSION' , $G_PGLOB->has_permission(6008) );
        $G_PGLOB->smarty->display('wms/goodsStore_list.tpl');

    }

}

